﻿using System.Linq;
using System.Text;
using System.Web.Routing;

namespace Basmus.Values.Web
{
    public static class ValueProviderExtensions
    {
        public static string ToQueryString(this IValuesProvider<string> stringValuesProvider)
        {
            return BuildQueryString(stringValuesProvider, false, null);
        }

        public static string ToQueryStringWithMultiValues(this IValuesProvider<string> stringValuesProvider, string multiValuesSeparator)
        {
            return BuildQueryString(stringValuesProvider, true, multiValuesSeparator);

        }

        public static RouteValueDictionary ToRouteValueDictionary(this IValuesProvider<string> stringValuesProvider)
        {
            var values = stringValuesProvider
                .GetNames()
                .ToDictionary(
                    x => x.ToLower(),
                    x =>
                    {
                        var stringValues = stringValuesProvider.GetValues(x);
                        if (stringValues != null)
                        {
                            var stringValuesArray = stringValues.ToArray();
                            if (stringValuesArray.Length > 0)
                            {
                                return (object)stringValuesArray.Aggregate((a, b) => a + "," + b);
                            }
                        }
                        return null;
                    }
                );
            return new RouteValueDictionary(values);
        }

        public static string BuildQueryString(this IValuesProvider<string> stringValuesProvider, bool multiValues, string multiValuesSeparator)
        {
            var sb = new StringBuilder();
            var names = stringValuesProvider.GetNames();
            var isFirstParameter = true;
            foreach (var name in names)
            {
                if (isFirstParameter)
                {
                    isFirstParameter = false;
                }
                else
                {
                    sb.Append("&");
                }
                var values = stringValuesProvider.GetValues(name);
                if (values == null)
                {
                    sb.AppendFormat(name);
                }
                else
                {
                    sb.AppendFormat("{0}=", name);
                    var isFirstValue = true;
                    foreach (var value in values)
                    {
                        if (isFirstValue)
                        {
                            sb.Append(value);
                            isFirstValue = false;
                        }
                        else
                        {
                            if (multiValues)
                            {
                                sb.AppendFormat("{0}{1}", multiValuesSeparator, value);
                            }
                            else
                            {
                                sb.AppendFormat("&{0}={1}", name, value);
                            }
                        }
                    }
                }
            }
            return sb.ToString();
        }
    }
}